// synchronized 保证 原子性
class Counter {
    public int count = 0;
    public void add() {
        synchronized (this) {
            count++;
        }
    }
}
public class ThreadDemo11 {
    public static void main(String[] args) throws InterruptedException {
        final Counter counter = new Counter();

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                counter.add();
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                counter.add();
            }
        });
        // 启动两个线程
        t1.start();
        t2.start();
        // 等待
        t1.join();
        t2.join();
        // 等两个线程都结束后打印
        System.out.println(counter.count);
    }
}
